<?php

namespace App\Http\Controllers\Admin;


use Illuminate\Http\Response;
use App\Models\Admin\Article;
use App\Models\Admin\ArticleClass;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;


class ArticleController extends Controller
{
    /**
     * @var UserRepository
     */
    private $apage;


    /**
     * ApageController constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->article = new Article(); 
    }

    /**
     * 单页列表
     *
     * @return Response
     */
    public function index()
    {
        $apageObj = $this->article->orderBy('article_id', 'desc')->paginate(15, ['*'], 'pageNum');
        return view('admin.Article.index', ['apage' => $apageObj]);
    }

    /**
     * 创建单页
     *
     * @return Response
     */
    public function create(Request $request)
    {
        $articleClass = new ArticleClass();
//        $category =$articleClass::get();
        $cate= ArticleClass::get(); 
        if ($request->getMethod() == "POST") {

            $validator = Validator::make($request->all(),
                [
                    'article_title' => 'required|min:2|max:255',
                    'article_content' => 'required',
                ],
                [
                    'article_title.required' => '单页标题不能为空',
                    'article_title.min' => '单页标题内容不能小于2个汉字',
                    'article_title.max' => '单页标题内容不能大于100个汉字',
                    'article_content.required' => '单页内容不能为空',
                ]
            );

            if ($validator->fails()) {
                return view('admin.Article.create')->withErrors($validator);
            }
            $insert = $this->article;

            $insert->article_title = $request->input('article_title');
            $insert->article_abstract = $request->input('article_abstract');
            $insert->article_class_id = $request->input('article_class_id');
            $insert->article_content = $request->input('article_content');
            $insert->article_image = $request->input('article_image');
            $insert->article_keyword = $request->input('article_keyword');
            $insert->article_publish_time = strtotime($request->input('article_publish_time'));
            $insert->article_click = $request->input('article_click');
            $insert->article_sort = $request->input('article_sort');
            if ($insert->save()) {
                return redirect()->route('admin.Article')->with('success','创建成功!');
            } else {
                return view('admin.Article.create')->withErrors($insert->save());
            }

        }


        return view('admin.Article.create',['cate' => $cate]);


    }


    /**
     * 编辑单页
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit(Request $request,$id)
    {
        
        $cate= ArticleClass::get(); 
        $apageObj=$this->article->find($id);
        if (!$apageObj) {
            $errors='内容不存在！';
            return redirect()->route('admin.Article')->withErrors(compact('errors'));
        }

        if($request->getMethod()=="POST"){
            $apageObj->article_title = $request->input('article_title');
            $apageObj->article_abstract = $request->input('article_abstract');
            $apageObj->article_class_id = $request->input('article_class_id');
            $apageObj->article_content = $request->input('article_content');
            $apageObj->article_image = $request->input('article_image');
            $apageObj->article_keyword = $request->input('article_keyword');
            $apageObj->article_publish_time = strtotime($request->input('article_publish_time'));
            $apageObj->article_click = $request->input('article_click');
            $apageObj->article_sort = $request->input('article_sort');
            $apageObj->update();
            return redirect()->route('admin.Article')->with('success','保存成功!');

        }
        return view('admin.Article.edit', ['apage'=>$apageObj,'cate'=>$cate]);
    }


    /**
     * 删除单页
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $apageObj=$this->article->find($id);
        if (!$apageObj) {
            $errors='内容不存在！';
            return redirect()->route('admin.Article')->withErrors(compact('errors'));
        }
        $apageObj->delete($id);

        return redirect()->route('admin.Article')->with('success','删除成功!');
    }


}
